/**
**/
'use strict';
var express = require('express');
var router = express.Router();
var request = require('request');
var async = require('async');
var logger = require('../logger');

/**
 * Get the list of news of gdut
 */

function fetchData(req, res, next) {
    const pageNo = req.query.pageNo || 1;
    
    const baseUrl = 'http://weixin.zhaobenshu.com/WeiXin/gdut/page/news/interface/news_ifp_GetNewsList.php?pageNo='+pageNo+'&x=jQuery1910814496892504394_1482383260512&_=1482383260513'
    let result = [];    // 结果数据
    logger.debug('----------  start fetch data  ----------');
    request.post(baseUrl, function(err, sres){
        if(err){
            return logger.error('ERROR:', err);
        }

         logger.debug('----------  fetch first page success!  ----------');

         const firstData = JSON.stringify(sres.body);
         result = JSON.parse("\""+firstData.slice(43,-3)+"\"");

         res.send(result)
    });   
}


/**
 * Get the news's detail of gdut News
 */
function fetchDetailData(req, res, next) {
    const NewsRd = req.query.NewsRd || 'WUVZU420CF';
    
    const baseUrl = 'http://weixin.zhaobenshu.com/WeiXin/gdut/page/news/interface/news_ifp_GetNewsDetail.php?NewsRd='+NewsRd
    
    logger.debug('----------  start fetch Detail data  ----------');
    request.post(baseUrl, function(err, sres){
        if(err){
            return logger.error('ERROR:', err);
        }

         logger.debug('----------  fetch first page success!  ----------');

         //sres--> 结果数据
         res.send(sres.body);
    });   
}

module.exports = {fetchData,fetchDetailData};
